//
//  ZhuCeViewController.m
//  ZhouProject
//
//  Created by Dlrj on 14-12-15.
//  Copyright (c) 2014年 admin. All rights reserved.
//

#import "ZhuCeViewController.h"
#import "LoadingHelper.h"
#import "NetWorkingHelper.h"
#import "XiaLa.h"
@interface ZhuCeViewController ()

@end

@implementation ZhuCeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册";
    self.navigationItem.hidesBackButton = YES;
    _row = 0;
    UILabel * labPhone = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(100), kW(80), kH(25))];
    labPhone.text = @"手机号:";
    labPhone.textColor = [UIColor blackColor];
    [self.view addSubview:labPhone];
    
    UILabel * labPassword = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(150), kW(80), kH(25))];
    labPassword.text = @"密码:";
    labPassword.textColor = [UIColor blackColor];
    [self.view addSubview:labPassword];
    
    UILabel * labPasswordconfirm = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(200), kW(80), kH(25))];
    labPasswordconfirm.text = @"确认密码:";
    labPasswordconfirm.textColor = [UIColor blackColor];
    [self.view addSubview:labPasswordconfirm];
    
    UILabel * labType = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(250), kW(80), kH(25))];
    labType.text = @"类别:";
    labType.textColor = [UIColor blackColor];
    [self.view addSubview:labType];
    
    UILabel * lab4s = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(300), kW(80), kH(25))];
    lab4s.text = @"4S店:";
    lab4s.textColor = [UIColor blackColor];
    [self.view addSubview:lab4s];
    
    UILabel * labZone = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(350), kW(80), kH(25))];
    labZone.text = @"区域:";
    labZone.textColor = [UIColor blackColor];
    [self.view addSubview:labZone];
    
    _txtPhone = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(95), kW(200), kH(33.5))];
    _txtPhone.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtPhone.returnKeyType = UIReturnKeyDone;
    _txtPhone.delegate = self;
    _txtPhone.keyboardType = UIKeyboardTypeNumberPad;
    _txtPhone.placeholder = @"有效的手机号";
    UILabel *leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtPhone setLeftView:leftView];
    [_txtPhone setLeftViewMode:UITextFieldViewModeAlways];
    _txtPhone.layer.cornerRadius = 5.0;
    _txtPhone.layer.borderWidth = 1.0f;//边框宽度
    _txtPhone.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    _txtPhone.clipsToBounds = YES;//内容不超出边框
    _txtPhone.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:_txtPhone];
    
    //            UIButton * btnhuoqu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //            btnhuoqu.frame = CGRectMake(kW(240), kH(100), kW(60), kH(25));
    //            [btnhuoqu setTitle:@"获取" forState:UIControlStateNormal];
    //            btnhuoqu.backgroundColor = [UIColor whiteColor];
    //            [btnhuoqu setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //            btnhuoqu.layer.borderWidth = 1.0f;//边框宽度
    //            btnhuoqu.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    //            btnhuoqu.layer.cornerRadius = 5.0;//设置圆角
    //            btnhuoqu.clipsToBounds = YES;//内容不超出边框
    //            [btnhuoqu addTarget:self  action:@selector(clickhuoqu) forControlEvents:UIControlEventTouchUpInside];
    //            [self.view addSubview:btnhuoqu];
    
    
    
    _txtPassword = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(145), kW(200), kH(33.5))];
    _txtPassword.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtPassword.returnKeyType = UIReturnKeyDone;
    _txtPassword.delegate = self;
    _txtPassword.placeholder = @"密码";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtPassword setLeftView:leftView];
    [_txtPassword setLeftViewMode:UITextFieldViewModeAlways];
    _txtPassword.secureTextEntry = YES;
    _txtPassword.clearButtonMode = UITextFieldViewModeAlways;
    _txtPassword.layer.cornerRadius = 5.0;
    _txtPassword.layer.borderWidth = 1.0f;//边框宽度
    _txtPassword.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    _txtPassword.clipsToBounds = YES;//内容不超出边框
    [self.view addSubview:_txtPassword];
    
    _txtPasswordconfirm = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(195), kW(200), kH(33.5))];
    _txtPasswordconfirm.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtPasswordconfirm.returnKeyType = UIReturnKeyDone;
    _txtPasswordconfirm.delegate = self;
    _txtPasswordconfirm.placeholder = @"确认密码";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtPasswordconfirm setLeftView:leftView];
    [_txtPasswordconfirm setLeftViewMode:UITextFieldViewModeAlways];
    _txtPasswordconfirm.secureTextEntry = YES;
    _txtPasswordconfirm.clearButtonMode = UITextFieldViewModeAlways;
    _txtPasswordconfirm.layer.cornerRadius = 5.0;
    _txtPasswordconfirm.layer.borderWidth = 1.0f;//边框宽度
    _txtPasswordconfirm.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    _txtPasswordconfirm.clipsToBounds = YES;//内容不超出边框
    [self.view addSubview:_txtPasswordconfirm];
    
    _txtType = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(245), kW(200), kH(33.5))];
    _txtType.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtType.returnKeyType = UIReturnKeyDone;
    _txtType.delegate = self;
    _txtType.placeholder = @"类别";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtType setLeftView:leftView];
    [_txtType setLeftViewMode:UITextFieldViewModeAlways];
    _txtType.layer.cornerRadius = 5.0;
    _txtType.layer.borderWidth = 1.0f;//边框宽度
    _txtType.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    _txtType.clipsToBounds = YES;//内容不超出边框
    [self.view addSubview:_txtType];
    
    _txt4S = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(295), kW(200), kH(33.5))];
    _txt4S.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txt4S.returnKeyType = UIReturnKeyDone;
    _txt4S.delegate = self;
    _txt4S.placeholder = @"4S店";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txt4S setLeftView:leftView];
    [_txt4S setLeftViewMode:UITextFieldViewModeAlways];
    _txt4S.clearButtonMode = UITextFieldViewModeAlways;
    _txt4S.layer.cornerRadius = 5.0;
    _txt4S.layer.borderWidth = 1.0f;//边框宽度
    _txt4S.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    _txt4S.clipsToBounds = YES;//内容不超出边框
    [self.view addSubview:_txt4S];
    
    _txtZone = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(345), kW(200), kH(33.5))];
    _txtZone.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtZone.returnKeyType = UIReturnKeyDone;
    _txtZone.delegate = self;
    _txtZone.placeholder = @"区域";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtZone setLeftView:leftView];
    [_txtZone setLeftViewMode:UITextFieldViewModeAlways];
    _txtZone.clearButtonMode = UITextFieldViewModeAlways;
    _txtZone.layer.cornerRadius = 5.0;
    _txtZone.layer.borderWidth = 1.0f;//边框宽度
    _txtZone.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;//边框颜色
    _txtZone.clipsToBounds = YES;//内容不超出边框
    [self.view addSubview:_txtZone];
    
    UIButton * btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLogin.frame = CGRectMake(kW(40), kH(450), kW(100), kH(35));
    [btnLogin setBackgroundImage:[UIImage imageNamed:@"hb4"] forState:UIControlStateNormal];
    [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnLogin setTitle:@"注册" forState:UIControlStateNormal];
    btnLogin.layer.cornerRadius = 5.0;//设置圆角
    btnLogin.clipsToBounds = YES;//内容不超出边框
    [btnLogin addTarget:self  action:@selector(clickzhuce) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin];
    
    UIButton * btnReg = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnReg.frame = CGRectMake(kW(180), kH(450), kW(100), kH(35));
    [btnReg setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReg setBackgroundImage:[UIImage imageNamed:@"hb1"] forState:UIControlStateNormal];
    [btnReg setTitle:@"取消" forState:UIControlStateNormal];
    btnReg.layer.cornerRadius = 5.0;//设置圆角
    btnReg.clipsToBounds = YES;//内容不超出边框
    [btnReg addTarget:self  action:@selector(clickquxiao) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnReg];
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.showsSelectionIndicator=YES;
    _txtType.inputView = _pickerView;
    
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];
    
    [topView setItems:buttonsArray];
    [_txtType setInputAccessoryView:topView];
}

-(void)dismissKeyBoard
{

    [_txtType resignFirstResponder];
    
}
-(void)clickhuoqu
{
    
}
-(void)clickzhuce
{
    if (_txtPhone.text.length == 0) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"注册失败" message:@"请填写手机号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else if(![[_txtPhone.text substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"1"]|| _txtPhone.text.length!= 11)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"注册失败" message:@"请填写正确的手机号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }

    else if(_txtPassword.text.length == 0)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"注册失败" message:@"请填写密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else if(_txtPassword.text.length < 6)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"注册失败" message:@"密码至少6位" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }

    else if(![_txtPassword.text isEqualToString:_txtPasswordconfirm.text])
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"注册失败" message:@"两次密码不一致" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else if(_txtType.text.length == 0)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"注册失败" message:@"请选择类别" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else if(_txt4S.text.length == 0)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"注册失败" message:@"请填写4S店" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else if(_txtZone.text.length == 0)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"注册失败" message:@"请填写区域" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
    else
    {
        [LoadingHelper showLoadingWithView:self.view];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString * str = [NetWorkingHelper ZhuCeWithuserName:_txtPhone.text userPwd:_txtPassword.text categoryId:_categoryId shopName:_txt4S.text shopArea:_txtZone.text];
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [LoadingHelper hiddonLoadingWithView:self.view];
                if ([str isEqualToString:@"0"]) {
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    alert.tag = 100;
                    [alert show];
                }
                else if([str isEqualToString:@"1"])
                {
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    [alert show];
                }
                else if([str isEqualToString:@"2"])
                {
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"申请账号重复" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    [alert show];
                }
            });
            
        });
    }
   
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 100) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (alertView.tag == 11) {
        [self dismissKeyBoard];
    }
}
-(void)clickquxiao
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_txtPhone resignFirstResponder];
    [_txtPassword resignFirstResponder];
    [_txtPasswordconfirm resignFirstResponder];
    [_txtType resignFirstResponder];
    [_txt4S resignFirstResponder];
    [_txtZone resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_txtPhone resignFirstResponder];
    [_txtPassword resignFirstResponder];
    [_txtPasswordconfirm resignFirstResponder];
    [_txtType resignFirstResponder];
    [_txt4S resignFirstResponder];
    [_txtZone resignFirstResponder];
    return YES;
}
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == _txtPhone) {              //手机号：length_11
        if (range.location >= 11) {
            return NO;
        }
        return YES;
    }
    else if (textField == _txtPassword || textField == _txtPasswordconfirm){           //用户名：length_15
        if (range.location >= 15) {
            return NO;
        }
        return YES;
    }
    else {
        return YES;
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (kScreenHeight!= 480) {
        if (textField == _txt4S) {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.center = CGPointMake(kScreenWidth / 2, self.view.center.y -  kH(40));
            } completion:nil];
        }else if(textField == _txtZone)
        {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.center = CGPointMake(kScreenWidth / 2, self.view.center.y -  kH(80));
            } completion:nil];
        }

    }
    else
    {
        if (textField == _txt4S) {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.center = CGPointMake(kScreenWidth / 2, self.view.center.y -  50);
            } completion:nil];
        }else if(textField == _txtZone)
        {
            [UIView animateWithDuration:0.25 animations:^{
                self.view.center = CGPointMake(kScreenWidth / 2, self.view.center.y -  100);
            } completion:nil];
        }

    }
    if (textField == _txtType) {
        
        [LoadingHelper showLoadingWithView:self.view];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            _pickerData = [NetWorkingHelper XiaLa];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (_pickerData.count > 0) {
                    XiaLa * model = [_pickerData objectAtIndex:_row];
                    _txtType.text = model.strcategoryName;
                    _categoryId = model.strcategoryId;
                    [_pickerView reloadAllComponents];
                    [_txtType reloadInputViews];
                }
                else {
                    _txtType.text = @"";
                    [_pickerView reloadAllComponents];
                    [_txtType reloadInputViews];
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请求用户类别失败，请检查网络后重试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    alert.tag = 11;
                    [alert show];
                }
                
                [LoadingHelper hiddonLoadingWithView:self.view];
            });
        });
    }    
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == _txt4S) {
        [UIView animateWithDuration:0.25 animations:^{
            self.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
        } completion:nil];
    }else if(textField == _txtZone)
    {
        [UIView animateWithDuration:0.25 animations:^{
            self.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
        } completion:nil];
    }

}


-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    XiaLa * model = [_pickerData objectAtIndex:row];
    return model.strcategoryName;

}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;//有多少列
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    return _pickerData.count;
 
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    XiaLa * model = [_pickerData objectAtIndex:row];
    _txtType.text = model.strcategoryName;
    _categoryId = model.strcategoryId;
    _row = row;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
